﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class __Login : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["SystemRegInfo"] == "SystemReg")
        {
            if (PublicFunction.SystemRegister())
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, "系统注册成功！", "Default.aspx");

                return;
            }
        }

        if (Request["SystemRegInfo"] == "SystemNoReg")
        {
            if (PublicFunction.SystemNoRegister())
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, "取消注册成功！", "Default.aspx");

                return;
            }
        }

        ShoveEL._Web.Session.ClearSession(ShoveEL._Web.WebConfig.GetAppSettingsString("pSystemPreFix") + "User");
        ShoveEL._Web.Session.ClearSession(ShoveEL._Web.WebConfig.GetAppSettingsString("pSystemPreFix") + "UserOffice");

        tbPassword.Attributes.Add("value", tbPassword.Text);
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }

    #endregion

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string ReturnDescription = "";

        int Result = new Login().LoginSubmit(this.Page, tbName.Text, PublicFunction.EncryptPassword(tbPassword.Text), tbCheckCode.Text.Trim(), ref ReturnDescription);

        if (Result < 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, ReturnDescription);

            return;
        }

        new Login().GoToRequestLoginPage("~/Default.aspx");
    }

    protected void lbPassword_Click(object sender, EventArgs e)
    {
        if (tbName.Text.Trim() == "")
        {
            tbCheckCode.Text = "";
            ShoveEL._Web.JavaScript.Alert(Page, "请输入消费ID！");
            return;
        }

        string CheckCode = "";
        try
        {
            CheckCode = this.Session["ShoveWebUI_CheckCode_CheckCode"].ToString();
        }
        catch
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "验证码输入错误！");
            return;
        }

        if (tbCheckCode.Text.ToUpper() != CheckCode.ToUpper())
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "验证码输入错误！");
            return;
        }

        System.Threading.Thread.Sleep(100);

        User user = new User();
        user.Name = tbName.Text.Trim();

        SqlConnection conn = ShoveEL.Database.MSSQL.CreateDataConnection();
        if (conn == null)
        {
            this.Response.Redirect("../Error.aspx?ErrorNumber=01", true);
            return;
        }

        string Return = "";
        int Result = user.GetUserInfoByName(ref Return);
        conn.Close();

        switch (Result)
        {
            case -1:
                //tbCheckCode.Text = "";
                ShoveEL._Web.JavaScript.Alert(this.Page, "消费ID不存在！");

                break;
            case -3:
                //tbCheckCode.Text = "";
                ShoveEL._Web.JavaScript.Alert(this.Page, "消费ID被限制登录！");

                break;
            case 0:
                if (user.Email == "")
                {
                    //tbCheckCode.Text = "";
                    ShoveEL._Web.JavaScript.Alert(this.Page, "您没有设置可以接收密码的邮箱，请与我们联系已取回密码！");
                    return;
                }
                else
                {
                    int sendMailResult = PublicFunction.SendEmail(user.Email, "您好，您在星科国际集团 的密码已找回，请查收！", "消费ID：" + user.Name + " 密码：" + user.Password + "，二级密码：" + user.Password2 + "。为了您的帐号安全，请及时登录到我们网站进行修改，谢谢！");
                    if (sendMailResult != 0)
                    {
                        ShoveEL._Web.JavaScript.Alert(this.Page, "密码发送到您的邮箱失败！请重试。");
                    }
                    else
                    {
                        ShoveEL._Web.JavaScript.Alert(this.Page, "密码已经发送到您的邮箱，请查收！");
                    }
                }

                break;
        }
    }

    protected void lbTrans_Click(object sender, EventArgs e)
    {

    }

    protected void tbPassword_TextChanged(object sender, EventArgs e)
    {

    }
    
}